### PS - Money 

# set working directory 
setwd("~/Dropbox/Works_in_Progress/PS_Piece_Money")

#load libraries
library(readxl)
library(tidyverse)
library(Hmisc)
library(reshape2)
library(ggplot2)


# read in data 
complete_data <- read_csv("~/Dropbox/Works_in_Progress/PS_Piece_Money/complete_data.csv")


###### Creation of Figure 1 #####
#labels for facet wrap
raised_data <- complete_data %>% group_by(Race, Gender)%>% summarise( Average.totalcontributons = mean(total.receipts, na.rm = TRUE), Average.paccontributions = mean(total.pac.contribs, na.rm = TRUE), 
                                                                      Average.individuals = mean(total.indiv.contrib, na.rm = TRUE), Average.pccontribs = mean(total.pc.contribs, na.rm = TRUE))

##remove missing data 
raised_data <- na.omit(raised_data)

#combine data and find the average by race and gender for campaign finance data
raised_data_melt <- melt(raised_data, id = c("Race", "Gender"))
raised_data_melt$Gender <- factor(raised_data_melt$Gender, levels = c(0, 1), labels = c("Men", "Women"))

raised_data_gender <- subset(raised_data_melt, Gender == "Women")


raised_plot <- ggplot(data= raised_data_gender, aes(x= Race, y= value, fill = variable)) +
  geom_bar(position = "dodge", stat = "identity") + theme_bw() + 
  xlab("Race of Candidates") + ylab("Average Dollars") + # Set axis labels + 
  theme(axis.text.x= element_text(size = 10, angle = 30, hjust = 1)) + 
  theme(axis.text.y = element_text(size = 10)) +
  scale_y_continuous(limits = c(0, 120000))+ 
  scale_fill_grey(name = "Receipt Type", labels = c("Average total receipts","Average receipts from PACs", "Average receipts from individual donors", 
                                                    "Average receipts from party committees"))
raised_plot


##### Total Contribution Model #### 

# factor and relevel race so comparison group is Black women 
complete_data$Race <- relevel(complete_data$Race, ref = "Black or African American")

model.w1<- glm(log_total.receipts ~  Race*Incum.Chall + candidate_party + recipient.cfscore + seat  + pctfemaleleg + ranney4_control +  state_culture + fem_gov + pollib_median,family = "gaussian", 
               subset = (complete_data$Gender == 1), data = complete_data)
summary(model.w1)


library(sjPlot)
library(sjmisc)
library(sjlabelled)

tab_model(model.w1, show.est = TRUE, show.ci = FALSE, show.se = TRUE, show.p = TRUE)

get_model_data(model.w1, type = "int")


library(DAMisc)
library(effects)
e1 <- effect("Race*Incum.Chall", model.w1)

# pe1 <- plot(e1, as.table=T, layout=c(3,1),rotx=45,
#             colors=c("black", "black"), lwd=NA,
#             col.symbol=rgb(0,0,0,0.1),
#               main="Effect Plot of Women's Total Campaign Receipts by Race and Incumbency Status",
#               xlab="Race of Candidate",
#              ylab="Logged Total receipts")
          
#### components of graph 
# Race of candidiates 
x <- e1$x[,1]

# Model fit 
y <- e1$fit

# Upper limit of error bar
upper <- e1$upper

# lower limit of error bar
lower <- e1$lower

# Incumbency status 
group <- e1$x[,2]

# create table 
table <- data.frame(x, group, y, upper, lower)

# create graph 
ggplot(table, aes(x, y)) + 
 geom_errorbar(aes(ymin =lower, ymax= upper),
                width=.2,   position=position_dodge(.9)) + theme_bw() + 
  facet_wrap(~ group) + labs(x = "Race of Candidate", y = "Logged Total receipts")
    


